import corejuliano.designpattern.observer.*;
import corejuliano.designpattern.mvc.*;


class corejuliano.designpattern.mvc.Controller extends Observable
{
	private var model:Model
	
	public function Controller(id:String)
	{
		createModel(id);
	}
	
	public function createModel(id:String):Void
	{
		model = new Model(id);
	}
	
	public function getModel():Model
	{
		return model;
	}
	
	// Controler tem acesso aos observadores
	// esse metodo e feito para pegar desses observadores quais sao view para um update screen por exemplo
	public function getViews():Array
	{
		var views:Array = new Array();
		for (var i:Number = 0; i< observers.length; i++)
			if(observers[i] instanceof View)
				views.push(observers[i]);
				
		return views;
	}
	
	public function destroy():Void
	{
		for(var i:Number = getViews().length -1; i>=0 ; i--)
			getViews()[i].destroy();
		
		model = null;
	}
	
	public function toString(Void):String
	{
		return "corejuliano.designpattern.mvc.Controller";
	}
	
}